package com.chilli.cosima.data;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * DAO层基类 <功能详细描述>
 * 
 * @version [版本号, 2010-8-2]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class BaseHibernateDAO extends HibernateDaoSupport implements BaseDAO
{
	// 创建对象
	public Object createObject(Object entity)
	{
		return getHibernateTemplate().save(entity);
	}

	// 删除对象
	public void deleteObject(Object entity)
	{
		getHibernateTemplate().delete(entity);
		getHibernateTemplate().flush();
	}

	// 更新
	public void updateObject(Object entity)
	{
		getHibernateTemplate().saveOrUpdate(entity);
	}

	// 批量更新
	public void executeBatch(String hql, Object[] values)
	{
		getHibernateTemplate().bulkUpdate(hql, values);

	}

	// 普通hql查询
	@SuppressWarnings("unchecked")
	public List findObjectByHQL(String queryString)
	{
		return getHibernateTemplate().find(queryString);
	}

	// 分页查询
	@SuppressWarnings("unchecked")
	public List findObjectByHQL(String queryString, int current, int pageSize)
	{
		if (current > 0)
		{
			current = current - 1;
		}
		return getHibernateTemplate().findByExample(queryString, current,
				pageSize);
	}

	// 普通hql,带参查询
	@SuppressWarnings("unchecked")
	public List findObjectByHQL(String queryString, Object[] values)
	{
		return getHibernateTemplate().find(queryString, values);
	}

	// 带参分页查询
	@SuppressWarnings("unchecked")
	public List findObjectByHQL(final String queryString,
			final Object[] values, final int current, final int pageSize)
	{
		return getHibernateTemplate().executeFind(new HibernateCallback()
		{
			int cur = current;

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				if (cur > 0)
				{
					cur = cur - 1;
				}

				Query query = session.createQuery(queryString);
				query.setFirstResult(pageSize * cur);
				query.setMaxResults(pageSize);
				for (int i = 0; i < values.length; i++)
				{
					query.setParameter(i, values[i]);
				}

				return query.list();
			}
		});
	}

	// 查询出几条
	@SuppressWarnings("unchecked")
	public List findObjectByHQL(String queryString, int maxResult)
	{
		getHibernateTemplate().setMaxResults(maxResult);
		return getHibernateTemplate().find(queryString);
	}

	// 带参查询出几条
	@SuppressWarnings("unchecked")
	public List findObjectByHQL(String queryString, Object[] values,
			int maxResult)
	{
		getHibernateTemplate().setMaxResults(maxResult);
		return getHibernateTemplate().find(queryString, values);
	}

	// 查询单个对象
	@SuppressWarnings("unchecked")
	public Object getObjectById(Class clazz, Object id)
	{
		return getHibernateTemplate().get(clazz, (Serializable) id);
	}
}
